package cn.hdustea.shopNavigation.ui.view.main.beauty

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.adapter.BeautyItemAdapter
import cn.hdustea.shopNavigation.entity.Beauty
import cn.hdustea.shopNavigation.ui.contract.BeautyContract
import cn.hdustea.shopNavigation.ui.presenter.BeautyPresenter
import com.hdustea.base.base.BaseFragment
import com.hdustea.base.widget.CustomLoadMoreView
import com.hdustea.base.widget.SpacesItemDecoration
import kotlinx.android.synthetic.main.fragment_beauty.*

private const val ARG_PARAM1 = "type"

class BeautyFragment : BaseFragment<BeautyContract.Presenter>(), BeautyContract.View {
    private var type: String? = null
    private var beautyAdapter: BeautyItemAdapter? = null
    private var mCurrentPage: Int = 0

    companion object {
        fun newInstance(param1: String) =
                BeautyFragment().apply {
                    arguments = Bundle().apply {
                        putString(ARG_PARAM1, param1)
                    }
                }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            type = it.getString(ARG_PARAM1)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mPresenter?.getBeautyList(0, false)
    }

    private fun initRecyclerView() {
        beautyAdapter!!.animationEnable = true
        beautyAdapter!!.loadMoreModule.loadMoreView = CustomLoadMoreView()
        beautyAdapter!!.loadMoreModule.setOnLoadMoreListener {
            mPresenter?.getBeautyList(mCurrentPage + 1, true)
        }
        beautyAdapter!!.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        beautyAdapter!!.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    override fun initView() {
        val layoutManage = LinearLayoutManager(context)
        rv_beauty_frag.layoutManager = layoutManage
        rv_beauty_frag.addItemDecoration(SpacesItemDecoration(8))
        showLoading()
    }

    override fun getLayoutRes(): Int = R.layout.fragment_beauty

    override fun getPresenter(): BeautyContract.Presenter = BeautyPresenter(this)

    // 往recyclerList中添加item
    override fun setBeautyList(beautyList: MutableList<Beauty>, isRefresh: Boolean) {
        if (!isRefresh) {
            beautyAdapter = BeautyItemAdapter(R.layout.item_list_beauty, this.context!!)
            initRecyclerView()
            rv_beauty_frag.adapter = beautyAdapter
            beautyAdapter!!.setList(beautyList)
        } else {
            if (beautyList.isNotEmpty()) {
                mCurrentPage++
                beautyAdapter!!.addData(beautyList)
                beautyAdapter!!.loadMoreModule.loadMoreComplete()
            } else {
                beautyAdapter!!.loadMoreModule.loadMoreEnd()
            }
        }
    }

    override fun onError() {
        beautyAdapter!!.loadMoreModule.isEnableLoadMore = true
        beautyAdapter!!.loadMoreModule.loadMoreFail()
    }
}